package com.xxx.video.main.adapters;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.xxx.video.R;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.adapters.BaseViewHolder;
import com.xxx.video.commen.configs.UserManager;
import com.xxx.video.commen.dbs.utils.DbUtils;
import com.xxx.video.commen.utils.CountUtils;
import com.xxx.video.commen.utils.DownloadUtils;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.utils.RecyclerviewDownloadListener;
import com.xxx.video.commen.utils.ResourcesUtils;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.utils.ToastUtils;
import com.xxx.video.commen.views.ExpandableTextView;
import com.xxx.video.download.core.breakpoint.BreakpointInfo;
import com.xxx.video.main.beans.VideoDetailsBean;

import butterknife.BindView;

public class VideoDetailsAdapter extends BaseRecycleViewAdapter<Object, VideoDetailsAdapter.MyHolder> {

    private VideoDetailsBean contentInfo;

    public VideoDetailsAdapter(@NonNull Context context) {
        super(context);
    }

    @Override
    public MyHolder onCreateDefaultViewHolder(View view, int viewType) {
        return new MyHolder(view);
    }

    @Override
    public int getItemLayout(int viewType) {
        return R.layout.item_video_details_fragment_details;
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    @Override
    public boolean isSetBaseItemViewClickListener() {
        return false;
    }

    /**
     * set video data
     *
     * @param contentInfo data
     */
    public void setVideoData(VideoDetailsBean contentInfo) {
        this.contentInfo = contentInfo;
        notifyDataSetChanged();
    }


    public void starPlus() {
        if (contentInfo != null) {
            contentInfo.count = 1;
            contentInfo.contentInfo.fLikeCount++;
            notifyDataSetChanged();
        }
    }

    public void unlikePlus() {
        if (contentInfo != null) {
            contentInfo.count = 2;
            contentInfo.contentInfo.fUnlikeCount++;
            notifyDataSetChanged();
        }
    }

    public void collectionSuccess(boolean isCollection) {
        if (contentInfo != null) {
            contentInfo.status = isCollection;
            notifyDataSetChanged();
        }
    }

    class MyHolder extends BaseViewHolder {

        @BindView(R.id.tv_item_video_details_fragment_details_desc)
        ExpandableTextView tvItemVideoDetailsFragmentDetailsDesc;
        @BindView(R.id.tv_item_video_details_fragment_details_play_count)
        TextView tvItemVideoDetailsFragmentDetailsPlayCount;
        @BindView(R.id.tv_item_video_details_fragment_details_comment_count)
        TextView tvItemVideoDetailsFragmentDetailsCommentCount;
        @BindView(R.id.tv_item_video_details_fragment_details_time)
        TextView tvItemVideoDetailsFragmentDetailsTime;
        @BindView(R.id.tv_item_video_details_fragment_details_like_count)
        TextView tvItemVideoDetailsFragmentDetailsLikeCount;
        @BindView(R.id.tv_item_video_details_fragment_details_un_like_count)
        TextView tvItemVideoDetailsFragmentDetailsUnLikeCount;
        @BindView(R.id.tv_item_video_details_fragment_details_favorites)
        TextView tvItemVideoDetailsFragmentDetailsFavorites;
        @BindView(R.id.tv_item_video_details_fragment_details_download_count)
        TextView tvItemVideoDetailsFragmentDetailsDownloadCount;
        @BindView(R.id.tv_item_video_details_fragment_details_recommend_title)
        TextView tvItemVideoDetailsFragmentDetailsRecommendTitle;

        public MyHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void setData2View(int position) {
            VideoDetailsBean.ContentInfoBean contentInfoBean = contentInfo.contentInfo;
            /*
             * title
             */
            tvItemVideoDetailsFragmentDetailsDesc.setText(StringUtils.getInstance().getNotEmptyMsg(contentInfoBean.fTitle));
            /*
             * play count
             */
            tvItemVideoDetailsFragmentDetailsPlayCount.setText(CountUtils.getInstance().getCount(contentInfoBean.fPlayTimes));
            /*
             *comment count
             */
            tvItemVideoDetailsFragmentDetailsCommentCount.setText(CountUtils.getInstance().getCount(contentInfoBean.fCommentTimes));
            /*
             * time
             */
            tvItemVideoDetailsFragmentDetailsTime.setText(StringUtils.getInstance().getNotEmptyMsg(String.valueOf(contentInfoBean.fCreateTime)));
            /*
             * like count
             */
            tvItemVideoDetailsFragmentDetailsLikeCount.setText(StringUtils.getInstance().getNotEmptyMsg(String.valueOf(contentInfoBean.fLikeCount)));
            /*
             * un like count
             */
            tvItemVideoDetailsFragmentDetailsUnLikeCount.setText(StringUtils.getInstance().getNotEmptyMsg(String.valueOf(contentInfoBean.fUnlikeCount)));

            tvItemVideoDetailsFragmentDetailsLikeCount.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_item_video_details_fragment_details_like_count, 0, 0);
            tvItemVideoDetailsFragmentDetailsUnLikeCount.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_item_video_details_fragment_details_un_like_count, 0, 0);
            if (contentInfo.count == 1) {
                tvItemVideoDetailsFragmentDetailsLikeCount.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_item_video_details_fragment_details_like_count_check, 0, 0);
            }
            if (contentInfo.count == 2) {
                tvItemVideoDetailsFragmentDetailsUnLikeCount.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_item_video_details_fragment_details_un_like_count_check, 0, 0);
            }

            /*
             * download count
             */
            tvItemVideoDetailsFragmentDetailsDownloadCount.setText(StringUtils.getInstance().getNotEmptyMsg(String.valueOf(contentInfoBean.fDownloadTimes)));
            tvItemVideoDetailsFragmentDetailsFavorites.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_item_video_details_fragment_details_favorites, 0, 0);

            if (contentInfo.status) {
                tvItemVideoDetailsFragmentDetailsFavorites.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_item_video_details_fragment_details_favorites_press, 0, 0);
            }

            tvItemVideoDetailsFragmentDetailsRecommendTitle.setVisibility(View.GONE);
            if (!ListUtils.isEmpty(contentInfo.page.list)) {
                tvItemVideoDetailsFragmentDetailsRecommendTitle.setVisibility(View.VISIBLE);
            }
            tvItemVideoDetailsFragmentDetailsLikeCount.setOnClickListener(v -> {
                if (contentInfo.count == 1) {
                    return;
                }
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(null, 0, 1);
                }
            });
            tvItemVideoDetailsFragmentDetailsUnLikeCount.setOnClickListener(v -> {
                if (contentInfo.count == 2) {
                    return;
                }
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(null, 0, 2);
                }
            });
            tvItemVideoDetailsFragmentDetailsFavorites.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(null, 0, 5);
                }
            });
            tvItemVideoDetailsFragmentDetailsDownloadCount.setOnClickListener(v -> {
                /*
                 * download
                 */
                //if download .start download activity, ir download
                BreakpointInfo breakpointInfo = DownloadUtils.getInstance().getDownloadInfo(ResourcesUtils.getInstance().getDownloadPath(contentInfo.contentInfo.fVideo));
                if (breakpointInfo == null) {
                    if (UserManager.getInstance().isLogin()) {
                        if (DownloadUtils.getInstance().download(ResourcesUtils.getInstance().getDownloadPath(contentInfo.contentInfo.fVideo), new RecyclerviewDownloadListener(this))) {
                            DbUtils.getInstance().addDownloadHistoryBean(contentInfo.contentInfo.fId,
                                    contentInfo.contentInfo.fVideo,
                                    contentInfo.contentInfo.fTitle,
                                    contentInfo.contentInfo.fPicurl,
                                    contentInfo.contentInfo.fCreateTime,
                                    contentInfo.contentInfo.fPlayTimes,
                                    contentInfo.contentInfo.fCommentTimes);
                            ToastUtils.showSuccess(StringUtils.getInstance().getString(R.string.tip_download_start));
                            if (onItemClickListener != null) {
                                onItemClickListener.onItemClick(null, 0, 4);
                            }
                        }
                    } else {
                        if (onItemClickListener != null) {
                            onItemClickListener.onItemClick(null, 0, 4);
                        }
                    }
                } else {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(null, 0, 3);
                    }
                }
            });
        }
    }
}
